package com.app.couponsharelife.ui.main.me.profit

import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.hwangjr.rxbus.thread.EventThread
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.databinding.ActivityProfitBinding
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.dialog.WithdrawExplainDialog
import com.app.couponsharelife.ui.main.me.bindalipay.BindAlipayActivity
import com.app.couponsharelife.ui.main.me.phonenum.BindPhoneNumActivity
import com.app.couponsharelife.ui.main.me.profit.record.NotProfitRecordActivity
import com.app.couponsharelife.ui.main.me.profit.record.ProfitRecordActivity
import com.app.couponsharelife.ui.main.me.profit.record.WithdrawRecordActivity
import com.app.couponsharelife.ui.main.me.withdrawal.WithdrawalActivity
import com.app.couponsharelife.ui.viewmodel.MeViewModel
import com.app.couponsharelife.utils.AppUtils
import com.app.fuquan.data.bean.PageStatus
import kotlinx.android.synthetic.main.activity_profit.*
import java.util.HashMap

class ProfitActivity : BaseActivity() {

    override val viewLayoutId: Int get() = R.layout.activity_profit

    private var mViewModel: MeViewModel? = null
    private var aliPay=""
    private var mobile=""
    override fun initViews() {
        AppUtils.initImmersionBar(this,false)
        initClickEvent()
    }

    private fun initClickEvent() {
        (mViewDataBinding as ActivityProfitBinding).listener = View.OnClickListener {
            when (it.id) {
                R.id.ivBack -> finish()
                R.id.tvRightBtn -> {
                    WithdrawExplainDialog(this, R.style.DialogStyle, "提现规则", "每月 25 日可提现上个月的已结算【确认收货订单】金额。").show()
                }
                R.id.tvCopyUserWithDraw -> {
                    //getAlipayInfo()
                    mobile=AppUtils.getString(Contacts.USER_MOBILE,"")
                    if (mobile.isEmpty()){
                        showToastMsg("请先绑定手机号")
                        val intent=Intent(this, BindPhoneNumActivity::class.java)
                        intent.putExtra("Type","Me")
                        startActivity(intent)
                    }else{
                        aliPay=AppUtils.getString(Contacts.ALIPAY_NUMBER,"")
                        if (aliPay.isNotEmpty()) {
                            val intent = Intent(this, WithdrawalActivity::class.java)
                            //intent.putExtra("alipay", userCenterBean!!.alipay)
                            startActivity(intent)
                        } else {
                            showToastMsg("请先绑定支付宝账号")
                            val intent = Intent(this, BindAlipayActivity::class.java)
                            intent.putExtra("isWithdraw", true)
                            intent.putExtra("Mobile",mobile)
                            startActivity(intent)
                        }
                    }
                }
                R.id.clProfitRecordD -> startActivity(Intent(this, NotProfitRecordActivity::class.java))
                R.id.clWithdrawRecord -> startActivity(Intent(this, WithdrawRecordActivity::class.java))
                R.id.clProfitRecordY -> startActivity(Intent(this, ProfitRecordActivity::class.java))
                R.id.syjs -> {
                    WithdrawExplainDialog(this, R.style.DialogStyle, "上月结算", "指上月确认收货订单的奖励金额，每月 25 日结算至账户余额。").show()
                }
                R.id.syyg -> {
                    WithdrawExplainDialog(this, R.style.DialogStyle, "上月预估", "指上月已付款订单的预估奖励。").show()
                }
                R.id.byyg -> {
                    WithdrawExplainDialog(this, R.style.DialogStyle, "本月预估", "指本月已付款订单的预估奖励。").show()
                }
                R.id.jrnumber -> {
                    WithdrawExplainDialog(this, R.style.DialogStyle, "付款笔数", "指今日已付款订单的数量。").show()
                }
                R.id.jryg -> {
                    WithdrawExplainDialog(this, R.style.DialogStyle, "预估佣金", "指今日已付款订单的预估奖励。").show()
                }
                R.id.jrqt -> {
                    WithdrawExplainDialog(this, R.style.DialogStyle, "其他", "指今日平台产生的其他奖励。").show()
                }
                R.id.zrnumber -> {
                    WithdrawExplainDialog(this, R.style.DialogStyle, "付款笔数", "指昨日已付款订单的数量。").show()
                }
                R.id.zryg -> {
                    WithdrawExplainDialog(this, R.style.DialogStyle, "预估佣金", "指昨日已付款订单的预估奖励。").show()
                }
                R.id.zrqt -> {
                    WithdrawExplainDialog(this, R.style.DialogStyle, "其他", "指昨日平台产生的其他奖励。").show()
                }
            }
        }
    }

    override fun initViewModel() {
        if (mViewModel == null) mViewModel = ViewModelProviders.of(this).get(MeViewModel::class.java)
        if (!mViewModel!!.getUserProfit()!!.hasObservers()) {
            mViewModel!!.getUserProfit()!!.observe(this, Observer {
                if (it == null) {
                    dismissProgressDialog()
                    showToastMsg("数据出错啦")
                    return@Observer
                }
                when (it.status) {
                    PageStatus.Loading -> {
                        showProgressDialog("请稍候")
                    }
                    PageStatus.Error -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetUserProfit(hashMapOf(), false)
                        showToastMsg(parseErrorDate(it.error!!.message).tip)
                    }
                    PageStatus.Empty -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetUserProfit(hashMapOf(), false)
                        showToastMsg("数据出错啦")
                    }
                    PageStatus.Content -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetUserProfit(hashMapOf(), false)
                        if (it.data!!.code == 200 && it.data.data != null) {
                            tvUserBalance.text = it.data.data!!.balance
                            tvUserTotalSettleProfit.text = String.format("累计结算收益：¥%s", it.data.data!!.sumSettlement)
                            tvLastMonthTrueProfitMoney.text = it.data.data!!.lastMonthSettlement
                            tvLastMonthEvaluateProfitMoney.text = it.data.data!!.lastMonthForecast
                            tvThisMonthEvaluateProfitMoney.text = it.data.data!!.thisMonthForecast
                            tvTodayPayNum.text = it.data.data!!.todayPayNum
                            tvTodayEvaluateProfitMoney.text = it.data.data!!.todayForecast
                            tvTodayOtherProfitMoney.text = it.data.data!!.todayRests
                            tvYesterdayPayNum.text = it.data.data!!.yesterdayPayNum
                            tvYesterdayEvaluateProfitMoney.text = it.data.data!!.yesterdayForecast
                            tvYesterdayOtherProfitMoney.text = it.data.data!!.yesterdayRests
                        } else {
                            showToastMsg("获取失败")
                        }
                    }
                }
            })
            mViewModel!!.getAlipayInfo()!!.observe(this, Observer {
                if (it == null) {
                    dismissProgressDialog()
                    showToastMsg("数据出错啦")
                    return@Observer
                }
                when (it.status) {
                    PageStatus.Loading -> {
                        showProgressDialog("请稍候")
                    }
                    PageStatus.Error -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetAlipayInfo(hashMapOf(), false)
                        if (it.error!!.message == "账户不存在") {
                            val intent = Intent(this, BindAlipayActivity::class.java)
                            intent.putExtra("isWithdraw", true)
                            startActivity(intent)
                        } else
                            showToastMsg(parseErrorDate(it.error.message).tip)
                    }
                    PageStatus.Empty -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetAlipayInfo(hashMapOf(), false)
                        showToastMsg("数据出错啦")
                    }
                    PageStatus.Content -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetAlipayInfo(hashMapOf(), false)
                        if (it.data!!.code == 200 && it.data.data != null) {
                            if (it.data.data!!.aliPayAccount != "") {
                                val intent = Intent(this, WithdrawalActivity::class.java)
                                intent.putExtra("alipay", it.data.data)
                                startActivity(intent)
                            } else {
                                val intent = Intent(this, BindAlipayActivity::class.java)
                                intent.putExtra("isWithdraw", true)
                                startActivity(intent)
                            }
                        } else {
                            showToastMsg("获取失败")
                        }
                    }
                }
            })
        }
        loadData()
    }

    private fun loadData() {
        val map = HashMap<String, String>()
        map["userId"] =AppUtils.getString(Contacts.USERID, "")
        mViewModel?.freshGetUserProfit(map, true)
    }

    private fun getAlipayInfo() {
        val map = HashMap<String, String>()
        map["userId"] = AppUtils.getString(Contacts.USERID, "")
        mViewModel?.freshGetAlipayInfo(map, true)
    }

    @Subscribe(thread = EventThread.MAIN_THREAD, tags = [(Tag("freshBalance"))])
    fun receiveData(fresh: String) {
        loadData()
    }

    override fun onDestroy() {
        super.onDestroy()
        AppUtils.destroyImmersionBar(this)
    }
}
